package com.sky.service.impl;

import com.sky.constant.ShoppingCartConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PageResult;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.PrimitiveIterator;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //    1.判断当前加入购物车商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        //    2.如果存在，数量加一
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + ShoppingCartConstant.ADD_CART_NUMBER);
            shoppingCartMapper.updateNumber(shoppingCart1);
        } else {
            //    3.如果不存在，插入数据
            // 3.1 判断添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(ShoppingCartConstant.INTIIAL_CART_NUMBER);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }


    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        log.info("用户{}正在查看购物车", userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getByUserId(userId);
        return shoppingCarts;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        log.info("用户{}正在清空购物车", userId);
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        if(shoppingCarts!=null&&shoppingCarts.size()>0){
        //    如果删除的商品份数为1，则删除该条数据
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            Integer shoppingCart1Number = shoppingCart1.getNumber();
            if(shoppingCart1Number==ShoppingCartConstant.INTIIAL_CART_NUMBER){
                shoppingCartMapper.deleteByShoppingCartId(shoppingCart1.getId());
            }else {
                //如果删除商品份数大于1，则更新数量
                shoppingCart1.setNumber(shoppingCart1.getNumber()-ShoppingCartConstant.SUB_CART_NUMBER);
                shoppingCartMapper.updateNumber(shoppingCart1);
            }
        }
    }
}
